package health.lm.com.mine.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.RegexUtils;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.data.App;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.present.SettingMailPresenter;
import health.lm.com.mine.present.SettingNamePresenter;
import health.lm.com.network.utils.RxUtil;
import health.lm.com.router.ARouterConstant;

/**
 * 企业邮箱
 * @author Administrator
 */
@Route(path = ARouterConstant.SettingMailActivity)
public class SettingMailActivity extends XActivity<SettingMailPresenter> {

    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @Autowired
    String userName = "";
    @BindView(R2.id.et_username)
    EditText etUsername;
    @BindView(R2.id.tv_change_name)
    TextView tvChangeName;
    private String name;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_mail;
    }

    @Override
    public SettingMailPresenter newP() {
        return new SettingMailPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
        //返回按钮
        titlebar.setListener((v, action, extra) -> finish());
        //修改名称
        RxTextView.textChanges(etUsername)
                .compose(bindToLife())
                .subscribe(charSequence -> {
                    name = charSequence.toString().trim();
                    if (name.length() > 0) {
                        tvChangeName.setEnabled(true);
                    } else {
                        tvChangeName.setEnabled(false);
                    }
                });
        //提交修改
        RxView.clicks(tvChangeName)
                .throttleFirst(2, TimeUnit.SECONDS)
                .compose(bindToLife())
                .subscribe(o -> {
                    if(RegexUtils.isEmail(name)){
                        getP().setName(name);
                    }else {
                        getvDelegate().toastShort("请输入正确的邮箱");
                    }
                });
        etUsername.setText(userName);
        //将光标移至文字末尾
        etUsername.setSelection(userName.length());
    }


    public void setNameSuccess(String name) {
        App.getModel().setNick_name(name);
        setResult(ARouterConstant.SUCCESS, new Intent().putExtra(ARouterConstant.USER_NAME, name));
        finish();
    }

}
